<template>
  <div class="gallery-container">
    <transition name="fade" mode="out-in">
      <div v-if="!selectedImage" key="grid">
        <div class="gallery-box">
          <div class="toolbar">
            <button :class="{ active: mode === 'default' }" @click="mode = 'default'">详情</button>
            <button :class="{ active: mode === 'enlarge' }" @click="mode = 'enlarge'">放大</button>
            <button :class="{ active: mode === 'drag' }" @click="mode = 'drag'">拖动</button>
          </div>
          <div class="grid-wrapper scrollable">
            <draggable
                v-model="images"
                :disabled="mode !== 'drag'"
                item-key="name"
                class="grid-container"
                :class="{ dragmode: mode === 'drag' }"
            >
              <template #item="{ element, index }">
                <div
                    class="grid-item"
                    :class="{
                    enlarged: mode === 'enlarge' && enlargedSet.has(index),
                    draghandle: mode === 'drag',
                  }"
                    @click="
                    mode === 'default'
                      ? selectImage(element, index)
                      : mode === 'enlarge' && toggleEnlarge(index)
                  "
                >
                  <img :src="element.url" :alt="element.name" />
                  <div
                      v-if="mode === 'default'"
                      class="info-overlay"
                  >
                    <h4>{{ element.name }}</h4>
                    <p>{{ formatDateTime(element.uploadTime) }}</p>
                  </div>
                </div>
              </template>
            </draggable>
          </div>
        </div>
      </div>

      <div v-else key="detail" class="detail-view">
        <div class="left-panel scrollable">
          <div class="image-header">
            <h2>{{ selectedImage.name }}</h2>
            <p>{{ formatDateTime(selectedImage.uploadTime) }}</p>
          </div>
          <div class="image-large">
            <img :src="selectedImage.url" />
          </div>
          <div class="image-info">
            <p><strong>检测模型：</strong>器官识别模型</p>
            <p><strong>病灶数量：</strong><span class="danger">异常 2 处</span></p>
            <p><strong>置信指数：</strong><span class="success">95%</span></p>
            <p><strong>临床建议：</strong><span class="recommendation">建议进一步检查</span></p>
          </div>
          <button class="back-btn" @click="clearSelection">返回</button>
        </div>

        <div class="right-panel scrollable">
          <div
              v-for="(img, idx) in images"
              :key="idx"
              class="thumb"
              :class="{ active: idx === activeIndex }"
              @click="selectImage(img, idx)"
          >
            <img :src="img.url" />
            <div class="info-overlay">
              <h4>{{ img.name }}</h4>
              <p>{{ formatDateTime(img.uploadTime) }}</p>
            </div>
          </div>
        </div>

      </div>
    </transition>
  </div>
</template>

<script setup>
import { ref, watch } from 'vue'
import draggable from 'vuedraggable'

const images = ref([
  {
    name: '器官识别-2024-04',
    uploadTime: '2024-04-15 14:30:00',
    url: 'src/assets/demo1.jpg',
  },
  {
    name: '器官识别-2024-03',
    uploadTime: '2024-03-28 09:45:00',
    url: 'src/assets/login.jpg',
  },
  {
    name: '脑部病灶识别-2024-03',
    uploadTime: '2024-03-20 16:20:00',
    url: 'src/assets/demo1.jpg',
  },
  {
    name: '脑部病灶识别-2024-02',
    uploadTime: '2024-02-10 08:55:00',
    url: 'src/assets/demo1.jpg',
  },
])

const selectedImage = ref(null)
const activeIndex = ref(-1)
const mode = ref('default')
const enlargedSet = ref(new Set())

watch(mode, (newMode) => {
  if (newMode === 'enlarge') {
    enlargedSet.value.clear()
  }
})

function toggleEnlarge(index) {
  if (enlargedSet.value.has(index)) {
    enlargedSet.value.delete(index)
  } else {
    enlargedSet.value.add(index)
  }
}

function selectImage(image, index) {
  selectedImage.value = image
  activeIndex.value = index
}

function clearSelection() {
  selectedImage.value = null
  activeIndex.value = -1
}

function formatDateTime(dateStr) {
  const date = new Date(dateStr)
  return `${date.toLocaleDateString()} ${date.toLocaleTimeString('zh-CN', { hour12: false })}`
}
</script>

<style scoped>
.gallery-container {
  height: 90vh;
  background: #f0f2f5;
  display: flex;
  justify-content: center;
}

.gallery-box {
  background: #f8fafc;
  border-radius: 16px;
  padding: 1rem 2rem;
  width: 90vw;
  height: 85vh;
  margin: 0 auto;
  overflow-y: auto;
  margin-top: 2.5vh;
  border: 1px solid #e2e8f0;
  box-shadow: inset 4px 4px 12px rgba(0, 0, 0, 0.08),
  inset -4px -4px 12px rgba(255, 255, 255, 0.9),
  0 2px 6px rgba(0, 0, 0, 0.05);
}

.toolbar {
  display: flex;
  justify-content: flex-end;
  gap: 0.5rem;
  margin-bottom: 1rem;
}
.toolbar button {
  padding: 6px 12px;
  border-radius: 6px;
  background: #e2e8f0;
  border: none;
  cursor: pointer;
}
.toolbar button.active {
  background: #3b82f6;
  color: white;
}

.grid-wrapper {
  width: 100%;
}
.grid-container {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(220px, 1fr));
  gap: 1.5rem;
}
.grid-item {
  position: relative;
  background: white;
  border-radius: 12px;
  overflow: hidden;
  transition: all 0.3s ease;
  box-shadow: 0 3px 6px rgba(0, 0, 0, 0.05), inset 0 0 0 1px rgba(226, 232, 240, 0.3);
  aspect-ratio: 1 / 1;
  cursor: pointer;
}
.grid-item.enlarged {
  grid-column: span 2;
  grid-row: span 2;
}
.grid-item img {
  width: 100%;
  height: 100%;
  object-fit: cover;
  border-radius: 10px;
}
.info-overlay {
  position: absolute;
  bottom: 0;
  width: 100%;
  background: rgba(0, 0, 0, 0.5);
  color: white;
  padding: 0.6rem;
  font-size: 0.85rem;
  text-align: left;
  opacity: 0;
  transition: opacity 0.3s ease;
}
.grid-item:hover .info-overlay {
  opacity: 1;
}

.detail-view {
  height: 85vh;
  width: 90vw;
  display: flex;
  background: #ffffff;
  border-radius: 16px;
  overflow: hidden;
  margin-top: 2.5vh;
  box-shadow: inset 3px 3px 8px rgba(0, 0, 0, 0.06), inset -3px -3px 8px rgba(255, 255, 255, 0.9);
}
.left-panel {
  flex: 1;
  padding: 30px;
  overflow-y: auto;
  background: linear-gradient(to bottom, #ffffff 0%, #f8fafc 100%);
}
.right-panel {
  width: 260px;
  background: #f8fafc;
  padding: 20px;
  overflow-y: auto;
  border-left: 1px solid #e2e8f0;
}
.thumb {
  border: 2px solid transparent;
  border-radius: 8px;
  overflow: hidden;
  cursor: pointer;
  position: relative;
  margin-bottom: 1rem;
}
.thumb.active {
  border-color: #3b82f6;
  box-shadow: 0 2px 8px rgba(59, 130, 246, 0.25);
}
.thumb img {
  width: 100%;
  height: 100px;
  object-fit: cover;
}

.thumb:hover .info-overlay {
  opacity: 1;
}

.image-header h2 {
  margin-bottom: 0.5rem;
}
.image-large img {
  width: 100%;
  max-height: 500px;
  object-fit: contain;
  border-radius: 12px;
  border: 1px solid #e2e8f0;
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.08);
  margin-bottom: 1.5rem;
}
.image-info {
  background: #f8fafc;
  padding: 1.5rem;
  border-radius: 12px;
  line-height: 1.8;
  margin-bottom: 2rem;
  border: 1px solid #e2e8f0;
}
.back-btn {
  background: linear-gradient(to right, #3b82f6, #2563eb);
  color: white;
  padding: 0.8rem 1.5rem;
  border: none;
  border-radius: 8px;
  cursor: pointer;
}
.danger { color: #dc2626; }
.success { color: #16a34a; }
.recommendation { color: #0ea5e9; }
</style>
